package com.usian.behavior.listener;

import com.alibaba.fastjson.JSON;
import com.usian.behavior.service.ApFollowBehaviorService;
import com.usian.model.behavior.dtos.FollowBehaviorDto;
import com.usian.model.common.constants.FollowBehaviorConstants;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.Optional;

@Component
public class BehaviorKafkaListener {


    @Autowired
    private ApFollowBehaviorService apFollowBehaviorService;


    @KafkaListener(topics = {FollowBehaviorConstants.FOLLOW_BEHAVIOR_TOPIC})
    private void listenNewsScan(ConsumerRecord<?,?>consumerRecord){
        Optional<? extends ConsumerRecord<?, ?>> recordOptional = Optional.ofNullable(consumerRecord);
        if (recordOptional.isPresent()){
            FollowBehaviorDto dto =
                    JSON.parseObject((String) consumerRecord.value(), FollowBehaviorDto.class);
            apFollowBehaviorService.saveFollowBehavior(dto);



        }
    }
}
